package com.yzh.openai.trigger.job;

import com.google.common.eventbus.EventBus;
import com.yzh.lantu.common.PayUtils;
import com.yzh.lantu.model.wx.request.WxNativeQueryOrderRequest;
import com.yzh.lantu.model.wx.response.WxNativeQueryOrderResponse;
import com.yzh.lantu.service.wx.NativePayService;
import com.yzh.openai.domain.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 检测用户支付了但是库表没有更新的订单
 */
@Slf4j
@Component
public class NoPayNotifyJob {
    @Value("${lantu.wxpay.config.mch-id}")
    private String mchid;
    @Value("${lantu.wxpay.config.partner-key}")
    private String partnerKey;
    @Resource
    private IOrderService orderService;
    @Resource
    private NativePayService nativePayService;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //@Scheduled(cron = "0/6 0 * * * ?")
    public void exec() {
        try {
            if (null == nativePayService) {
                log.info("定时任务【订单支付状态更新】 未配置微信支付相关参数 nativePayService is null!");
                return;
            }
            //查询下单后1分钟未支付的订单
            String orderId = orderService.queryNoPayNotifyOrder();
            if (StringUtils.isBlank(orderId)) {
                log.info("定时任务【订单支付状态更新】 暂无需要更新订单");
                return;
            }
            //查询蓝兔订单否成功支付
            WxNativeQueryOrderRequest request = new WxNativeQueryOrderRequest();
            request.setMch_id(mchid);
            request.setOut_trade_no(orderId);
            request.setTimestamp(dateFormat.format(new Date()));
            request.setSign(PayUtils.createSign(PayUtils.objectToMap(request), partnerKey));

            WxNativeQueryOrderResponse orderResponse = nativePayService.queryOrder(request);
            
            //查询微信那边是否成功支付
/*            for (String orderId : orderIdList) {
                QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
                request.setOutTradeNo(orderId);
                request.setMchid(mchid);
                Transaction transaction = nativePayService.queryOrderByOutTradeNo(request);
                //若订单未支付就不用管它
                if (!Transaction.TradeStateEnum.SUCCESS.equals(transaction.getTradeState())) {
                    log.info("定时任务【订单支付状态更新】 当前订单未支付 orderId：{} ", orderId);
                    continue;
                }
                //若用户支付了，库表中却没有更新，则更新
                String transactionId = transaction.getTransactionId();  //支付单号
                Integer total = transaction.getAmount().getTotal();
                BigDecimal totalAmount = new BigDecimal(total).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
                String successTime = transaction.getSuccessTime();
                //更新订单
                boolean success = orderService.changeOrderPaySuccess(orderId, transactionId, totalAmount, dateFormat.parse(successTime));
                if (success) eventBus.post(orderId);    //发货
            }*/
        } catch (Exception e) {
            log.error("定时任务【订单支付状态更新】失败，出现异常", e);
        }
    }
}
